package com.wxtl.dao;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Repository;

import com.wxtl.dao.base.HibernateBaseDao;
import com.wxtl.entity.Dog;
import com.wxtl.util.DataGridModel;
import com.wxtl.util.Page;
import com.wxtl.util.Status;

/**
 * 加密狗DAO
 * @author hc24
 *
 */
@Repository
public class DogDAO extends HibernateBaseDao {
	private static final Log log = LogFactory.getLog(DogDAO.class);

	/**
	 * 根据加密狗编号得到加密狗对象
	 * @param sn
	 * @return
	 */
	public Dog getDogBySn(String sn){
		List<Dog> list= getSession().createQuery("from Dog d where d.dogsn=?").setParameter(0, sn).list();
		if(list.size()>0)
			return list.get(0);
		return null;
	}
	
	/**
	 * 根据agent对象查询
	 * @param dgm
	 * @param agent
	 * @return
	 */
	public Page query(DataGridModel dgm,Dog dog, int dealFlag){
		StringBuilder hql=new StringBuilder();
		List<Object> params=new ArrayList<Object>();
		hql.append("from Dog d where 1=1");
		if(dog.getDogsn()!=null&&!"".equals(dog.getDogsn())){
			hql.append(" and d.dogsn=?");
			params.add(dog.getDogsn());
		}
		if(dog.getAgentsn()!=null&&!"".equals(dog.getAgentsn())){
			hql.append(" and d.agentsn=?");
			params.add(dog.getAgentsn());
		}
		if(dog.getCustsn()!=null&&!"".equals(dog.getCustsn())){
			hql.append(" and d.custsn=?");
			params.add(dog.getCustsn());
		}
		if(dog.getProCode()!=null&&!"".equals(dog.getProCode())){
			hql.append(" and d.proCode=?");
			params.add(dog.getProCode());
		}
		
		if(dealFlag==Status.DEAL_DONE){
			hql.append(" and d.agentsn!=null");
		}else if(dealFlag == Status.DEAL_UNDONE){
			hql.append(" and d.agentsn=null");
		}
		
		return super.queryForList(hql.toString(), params.toArray(new Object[]{}), dgm.getPage(), dgm.getRows());
	}
	
	public Dog getDogById(int id){
		return (Dog) getHibernateTemplate().get(Dog.class, id);
	}
}